package com.zd.mole.site.mohurd.process;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.site.mohurd.entity.Ot_project_info;
import com.zd.mole.site.mohurd.service.SysDictService;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

/**
 * 项目数据/基本信息
 * @author ZhangDi
 *
 */
@Component
@Transactional
public class MohurdDataserviceQueryProjectProjectDetailHandler implements ProcessHandler {
	
	@PersistenceContext 
	private EntityManager em;
	@Resource
	private TaskService taskService;
	@Resource
	private SysDictService sysDictService;

	@Override
	public List<Task> handler(Task task, String text) {
		
		Ot_project_info project_info = new Ot_project_info();
		project_info.setId(task.getCode());
		project_info.setProjectNo(task.getCode());
		project_info.setProjectName(RegexUtils.find("<b title=\"" + INNER_HTML + "\"><i class=\"fa fa-cubes\"></i>", text));
		project_info.setProvinceProjectNo(RegexUtils.find("<dd><span>省级项目编号：</span>" + INNER_HTML + "</dd>", text));
		project_info.setProjectArea(RegexUtils.find("<dd><span>所在区划：</span>" + INNER_HTML + "</dd>", text));
		project_info.setDevelopmentDw(RegexUtils.find("<dd><span>建设单位：</span>" + INNER_HTML + "</dd>", text));
		project_info.setDevelopmentDwCode(RegexUtils.find("<span>建设单位组织机构代码（统一社会信用代码）：</span>" + INNER_HTML + "</dd>", text));
		String projectType = RegexUtils.find("<dd><span>项目分类：</span>" + INNER_HTML + "</dd>", text);
		projectType = sysDictService.findOrSaveByTypeLabel("oLife_ProjectType", projectType, "项目类别");
		project_info.setProjectType(projectType);
		String developmentType = RegexUtils.find("<dd><span>建设性质：</span>" + INNER_HTML + "</dd>", text);
		developmentType = sysDictService.findOrSaveByTypeLabel("oLife_ProBuldType", developmentType, "建设性质");
		project_info.setDevelopmentType(developmentType);
		String projectUsage = RegexUtils.find("<dd><span>工程用途：</span>" + INNER_HTML + "</dd>", text);
		projectUsage = sysDictService.findOrSaveByTypeLabel("oLife_ProUseType", projectUsage, "工程用途");
		project_info.setProjectUsage(projectUsage);
		project_info.setProjectInvestment(RegexUtils.find("<dd><span>总投资：</span>" + INNER_HTML + "</dd>", text));
		project_info.setTotalArea(RegexUtils.find("<dd><span>总面积：</span>" + INNER_HTML + "</dd>", text));
		String setProType = RegexUtils.find("<dd><span>立项级别：</span>" + INNER_HTML + "</dd>", text);
		setProType = sysDictService.findOrSaveByTypeLabel("oLife_ProLiTopType", setProType, "立项级别");
		project_info.setSetProType(setProType);
		project_info.setSetProCode(RegexUtils.find("<dd><span>立项文号：</span>" + INNER_HTML + "</dd>", text));
		project_info.setCreate_by("1");
		project_info.setCreate_date(new Date());
		project_info.setUpdate_by("1");
		project_info.setUpdate_date(new Date());
		project_info.setDel_flag("0");
		em.merge(project_info);
		//招投标
		String tab_ztb = RegexUtils.find("<div class=\"activeTinyTabContent\" id=\"tab_ztb\">" + INNER_HTML + "</div>", text);
		RegexUtils.findAll("data-url=\"(/dataservice/query/project/tenderInfo/\\d+)\"", tab_ztb)
			.forEach(s -> {
				Task t = new Task();
				t.setCode(task.getCode());
				t.setHostUrl(task.getHostUrl());
				t.setRequestUrl(s);
				t.setProcessHandlerClassName("MohurdDataserviceQueryProjectTenderInfoHandler");
				t.setStatus(TaskStatus.Ready);
				taskService.save(t);
			});
		//施工图审查
		String tab_sgtsc = RegexUtils.find("<div id=\"tab_sgtsc\">" + INNER_HTML + "</div>", text);
		RegexUtils.findAll("data-url=\"(/dataservice/query/project/censorInfo/\\d+)\"", tab_sgtsc)
			.forEach(s -> {
				Task t = new Task();
				t.setCode(task.getCode());
				t.setHostUrl(task.getHostUrl());
				t.setRequestUrl(s);
				t.setProcessHandlerClassName("MohurdDataserviceQueryProjectCensorInfoHandler");
				t.setStatus(TaskStatus.Ready);
				taskService.save(t);
		});
		//合同备案
		String tab_htba = RegexUtils.find("<div id=\"tab_htba\">" + INNER_HTML + "</div>", text);
		RegexUtils.findAll("data-url=\"(/dataservice/query/project/contractInfo/\\d+)\"", tab_htba)
			.forEach(s -> {
				Task t = new Task();
				t.setCode(task.getCode());
				t.setHostUrl(task.getHostUrl());
				t.setRequestUrl(s);
				t.setProcessHandlerClassName("MohurdDataserviceQueryProjectContractInfoHandler");
				t.setStatus(TaskStatus.Ready);
				taskService.save(t);
			});
		//施工许可
		String tab_sgxk = RegexUtils.find("<div id=\"tab_sgxk\" >" + INNER_HTML + "</div>", text);
		RegexUtils.findAll("data-url=\"(/dataservice/query/project/buildliseInfo/\\d+)\"", tab_sgxk)
			.forEach(s -> {
				Task t = new Task();
				t.setCode(task.getCode());
				t.setHostUrl(task.getHostUrl());
				t.setRequestUrl(s);
				t.setProcessHandlerClassName("MohurdDataserviceQueryProjectBuildliseInfoHandler");
				t.setStatus(TaskStatus.Ready);
				taskService.save(t);
			});
		//竣工验收备案
		String tab_jgysba = RegexUtils.find("div id=\"tab_jgysba\">" + INNER_HTML + "</div>", text);
		RegexUtils.findAll("data-url=\"(/dataservice/query/project/bafinishInfo/\\d+)\"", tab_jgysba)
			.forEach(s -> {
				Task t = new Task();
				t.setCode(task.getCode());
				t.setHostUrl(task.getHostUrl());
				t.setRequestUrl(s);
				t.setProcessHandlerClassName("MohurdDataserviceQueryProjectBafinishInfoHandler");
				t.setStatus(TaskStatus.Ready);
				taskService.save(t);
			});
		
		
		return null;
	}

}
